package wrapper

import (
	"gitee.com/ymofen/gobase"
)

var (
	NULL_OBJECT *SuperValue = nil
)

type SValue interface {
	StringByName(name string, def string) string
	String() string
	IsString() bool
	IsObject() bool
	Write2StrMap(strMap gobase.StrMap) int
	AsJSONStringEx(escape bool, format bool) string
	GetKeys() []string
	SetKeyValue(key string, val interface{})
	SetKeyValues(kvparse ...interface{})
}

func GetSVFromArgs(arg interface{}, idx int) *SuperValue {
	v0 := gobase.GetIdxValFromVarsDef(arg, idx, (*SuperValue)(nil))
	if v0 == nil {
		return nil
	}
	if sv, ok := v0.(*SuperValue); ok {
		return sv
	}
	return nil
}

type SetConfigIntf interface {
	SetConfig(conf *SuperValue) error
}

type IReloadConf interface {
	ReloadConf(conf *SuperValue) (err error)
}

type IReloadList interface {
	ReloadList(lst *SuperValue)
}

type ISVRequest interface {
	RequestSV(req *SuperValue, resp *SuperValue)
}

type IFillJsonStatus interface {
	FillJsonStatus(rec *SuperValue, args ...interface{}) bool
}

func TryFillJsonStatus(obj interface{}, rec *SuperValue, args ...interface{}) bool {
	if intf, ok := obj.(IFillJsonStatus); ok {
		return intf.FillJsonStatus(rec, args...)
	}
	return false
}

func SetConfig(obj interface{}, conf *SuperValue) (bool, error) {
	if intf, ok := obj.(SetConfigIntf); ok {
		err := intf.SetConfig(conf)
		return true, err
	} else {
		return false, nil
	}
}

/*
**
如果插件支持IReloadconf, 会返回true

	error obj执行Reloadconf返回的异常
*/
func TryReloadConf(obj interface{}, conf *SuperValue) (bool, error) {
	if intf, ok := obj.(IReloadConf); ok {
		return true, intf.ReloadConf(conf)
	}
	return false, nil
}
